package sword;

/**
 * @Author: 海琳琦
 * @Date: 2022/5/27 22:23
 */
public class Title68I {

    class TreeNode{
        int val;

        TreeNode left;

        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root.val > p.val && root.val > q.val) {
            //结果一定在左边
            return  lowestCommonAncestor(root.left, p, q);
        }
        if (root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }
        //一左一右，直接返回根节点
        return root;
    }

    public static void main(String[] args) {

    }
}
